package cn.edu.nuc.film.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.nuc.film.common.PageControl;
import cn.edu.nuc.film.common.Pager;
import cn.edu.nuc.film.common.Parameter;
import cn.edu.nuc.film.common.UnitHelper;
import cn.edu.nuc.film.dao.FunctionMapper;
import cn.edu.nuc.film.entity.Function;
import cn.edu.nuc.film.service.interfaces.FunctionService;

@Service
public class FunctionServiceImpl implements FunctionService {
	
	@Autowired
	private FunctionMapper functionMapper;
	
	/**
	 * 添加功能
	 */
	public int addFun(Function function) {
		int count = functionMapper.insert(function);
		if( count > 0 ){
			return count;
		}else{
			throw new RuntimeException("修改失败！");
		}
		
	}
	/**
	 * 功能列表
	 */
	public Pager<Function> funlist( PageControl pc,String name ) {
		/**
		 * 设置dao层的参数列表( @Param("start") int start,
		 * @Param("offset") int offset,@Param("filmName") String filmName )
		 */
		Parameter para = new Parameter();
		para.setStart( (pc.getCurrentindex() - 1) * pc.getPagesize());
		para.setOffset( pc.getPagesize());
		para.setName(name);
		//获取每页的具体数据
		List<Function> list = functionMapper.funlist(para);
		
		//设置总记录数
		int pagecount = 0;
		if( name != null ){
			pagecount = list.size();
		}else{
			pagecount = functionMapper.count();
		}
		/*System.out.println(pagecount);*/
		//测试是否获得
		/*System.out.println("-----------------filmlist------------------");
		System.out.println(list);*/
		Pager<Function> pager = UnitHelper.execlist(pagecount, list, pc);
		return pager;
	}
	
	/**
	 * 获取详细信息，通过fid来获取
	 */
	public Function detailFun(int fid) {
		Function function = functionMapper.detail(fid);
		return function;
	}
	
	/**
	 * 编辑功能，
	 */
	public int editFun(Function function) {
		int count = functionMapper.updateByFid(function);
		return count;
	}
	
	/**
	 * 删除功能
	 */
	public int deleteFun(int fid) {
		int count = functionMapper.deleteByFid(fid);
		if( count > 0){
			return count;
		}else{
			throw new RuntimeException("删除失败");
		}
		
	}
	

}
